package inits

import (
	"2302a/tiktok_business/config"
	"2302a/tiktok_business/global"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
)

func InitMysql() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	configuration := config.AppConf.MysqlYun
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		configuration.User, configuration.Password, configuration.Host, configuration.Port, configuration.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
	if err != nil {
		print(err)
		return
	}
	log.Println("MySQL连接成功")
}
